{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ec4fd4b3",
   "metadata": {},
   "source": [
    "# 字典的方法\n",
    "## 字典的查询\n",
    "> 1.in方法的查询  \n",
    "> 2.get方法的查询"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "cd438a79",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu1801': 'guosiwen', 'stu1802': 'liliumang', 'stu1803': 'jianglaoban'}\n"
     ]
    }
   ],
   "source": [
    "info = {\n",
    "    \"stu1801\":\"guosiwen\",\n",
    "    \"stu1802\":\"liliumang\",\n",
    "    \"stu1803\":\"jianglaoban\"\n",
    "}\n",
    "print(info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "b54ef5b7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查询 key 是否在字典当中 用 in \n",
    "\"stu1801\" in info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "2c73e433",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查询 key在不在的结果为True or False\n",
    "\"stu1807\" in info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "d1f831e4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'guosiwen'"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# get 方法 获取字典中的值（ key 对应的value内容）\n",
    "info.get(\"stu1801\")\n",
    "# get的用法：直接输出的话，如果没有这个值，运行会报错，但是加入get的话可以避免报错的问题"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "a8a84234",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "guosiwen\n"
     ]
    }
   ],
   "source": [
    "# 综上所述：字典的查询方法一般步骤如下：\n",
    "if \"stu1801\" in info:\n",
    "    print(info.get(\"stu1801\"))\n",
    "else:\n",
    "    print(\"您查询的内容不存在\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1dca6fea",
   "metadata": {},
   "source": [
    "## 字典的删除\n",
    "> 1.pop(）指定删除值  \n",
    "> 2.popitem（）随机删除值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "55ce3795",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1801': 'guosiwen', 'stu1802': 'liliumang', 'stu1803': 'jianglaoban'}"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "a862503c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'guosiwen'"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info.pop(\"stu1801\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "1d71e9fd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1802': 'liliumang', 'stu1803': 'jianglaoban'}"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "0d927592",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('stu1803', 'jianglaoban')"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info.popitem()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "62db232b",
   "metadata": {},
   "outputs": [],
   "source": [
    "info[\"stu1804\"] = \"yingying\"\n",
    "# 尝试在字典里面加入想要的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "e8c3653f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1802': 'liliumang', 'yingying': 'stu1804', 'stu1804': 'yingying'}"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info\n",
    "# 查看产生值的效果，疑问：为什么stu=name会产生两个值/name=stu只有一个值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "0f34f55a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'stu1804'"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info.pop(\"yingying\")\n",
    "# 删除多余的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "48eb8dca",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1802': 'liliumang', 'stu1804': 'yingying'}"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info\n",
    "# 查看字典现在情况"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "id": "2bbe8dd6",
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "'stu1802'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-88-f8d77bfdbe7d>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32mdel\u001b[0m \u001b[0minfo\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'stu1802'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      2\u001b[0m \u001b[1;31m# 尝试使用del来删除字典信息\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[1;31m# 出错\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mKeyError\u001b[0m: 'stu1802'"
     ]
    }
   ],
   "source": [
    "del info['stu1802']\n",
    "# 尝试使用del来删除字典信息\n",
    "# 出错"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a94dae40",
   "metadata": {},
   "source": [
    "## 用自定义值建立字典\n",
    "> 1.del 删除字典内容的方法  \n",
    "> 2.通过 （“字典”= “想要的值”）来添加字典内容  \n",
    "> 3.get的用法  \n",
    "> 4.clear清空字典  \n",
    "> 5.del dict 删除字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "id": "5551e2bb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'yingying'"
      ]
     },
     "execution_count": 104,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict = {\n",
    "    'stu1802':'liliumang',\n",
    "    'stu1804': 'yingying'\n",
    "}\n",
    "dict['stu1804']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "id": "570520fa",
   "metadata": {},
   "outputs": [],
   "source": [
    "del dict['stu1802']\n",
    "# 删除想要删除的值，并且只能删除一次，再次删除会报错"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "id": "7973b57a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1804': 'yingying'}"
      ]
     },
     "execution_count": 106,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "id": "21d9eabf",
   "metadata": {},
   "outputs": [],
   "source": [
    "dict['stu1805'] = 'guojunbin'\n",
    "# 在字典中添加内容"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "id": "5c75d484",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1804': 'yingying', 'stu1805': 'guojunbin'}"
      ]
     },
     "execution_count": 110,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "id": "5a2a5efd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'yingying'"
      ]
     },
     "execution_count": 114,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict['stu1804']\n",
    "# 输出正常值提取字典内容"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "id": "84c4a723",
   "metadata": {},
   "outputs": [],
   "source": [
    "# dict['stu1809']\n",
    "# 输出不存在的值会报错，可以使用get来解决报错的问题\n",
    "dict.get('stu1809')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "id": "21b4d148",
   "metadata": {},
   "outputs": [],
   "source": [
    "dict.clear()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "id": "51927c53",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{}"
      ]
     },
     "execution_count": 119,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4756e66e",
   "metadata": {},
   "source": [
    "## 字典的遍历——计算工资的发放[课外拓展]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "id": "d9ba9696",
   "metadata": {},
   "outputs": [],
   "source": [
    "salary = {'流氓':1200,'斯文':1100,'江老板':1000}\n",
    "workday = {'流氓':5,'斯文':6,'江老板':7}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "id": "d9cdf58c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6000\n",
      "6600\n",
      "7000\n",
      "总支付工资为： 19600\n"
     ]
    }
   ],
   "source": [
    "paid = 0\n",
    "for i in salary:\n",
    "    print(salary[i]*workday[i])\n",
    "    paid = paid + (salary[i]*workday[i])\n",
    "print('总支付工资为：', paid)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f35cedf4",
   "metadata": {},
   "source": [
    "# 字典的一般方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "id": "1352165a",
   "metadata": {},
   "outputs": [],
   "source": [
    "info = {\n",
    "    \"stu1801\":\"ZiQi\",\n",
    "    \"stu1802\":\"XiaoFeng\",\n",
    "    \"stu1803\":\"LiuYu\"\n",
    "}     # 写下字典库"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "id": "0eec1c67",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1801': 'ZiQi', 'stu1802': 'XiaoFeng', 'stu1803': 'LiuYu'}"
      ]
     },
     "execution_count": 123,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "id": "ae4f5626",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1801 ZiQi\n",
      "1802 XiaoFeng\n",
      "1803 LiuYu\n"
     ]
    }
   ],
   "source": [
    "# 循环遍历字典\n",
    "# 循环遍历的最大用途就是：给所有的值使用同样的 操作/方法\n",
    "stu_id_list = []\n",
    "for k,v in info.items():\n",
    "    print(k[3:],v)\n",
    "    stu_id_list.append(k[3:])\n",
    "    \n",
    "#     即是给所有的值都定下一个输出格式：键值对为(k[3],v),从key的序号[3]输出 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "id": "c7d27ec7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['1801', '1802', '1803']"
      ]
     },
     "execution_count": 125,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_id_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e05690ab",
   "metadata": {},
   "source": [
    "### 课后练习\n",
    "> 请大家把info的 value 目前的格式进行统一修改，修改的范式为： 姓 名 Zi Qi Xiao Feng"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f66e29f2",
   "metadata": {},
   "source": [
    "## fromkeys()方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 126,
   "id": "a3fb7d51",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function fromkeys:\n",
      "\n",
      "fromkeys(iterable, value=None, /) method of builtins.type instance\n",
      "    Create a new dictionary with keys from iterable and values set to value.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(info.fromkeys)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "id": "8698f940",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stud2101': 123456, 'stud2102': 123456, 'stud2103': 123456}"
      ]
     },
     "execution_count": 127,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 初始化统一值的字典信息\n",
    "学生初始密码={}\n",
    "学生初始密码.fromkeys([\"stud2101\",\"stud2102\",\"stud2103\"],123456)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e78ccbac",
   "metadata": {},
   "source": [
    "## setdefault()方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "id": "6ce8c8cc",
   "metadata": {},
   "outputs": [],
   "source": [
    "info.setdefault('stu1804')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 129,
   "id": "f6ff9d96",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'zhichao'"
      ]
     },
     "execution_count": 129,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info.setdefault('stu1805','zhichao')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "id": "a43e09eb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1801': 'ZiQi',\n",
       " 'stu1802': 'XiaoFeng',\n",
       " 'stu1803': 'LiuYu',\n",
       " 'stu1804': None,\n",
       " 'stu1805': 'zhichao'}"
      ]
     },
     "execution_count": 130,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "256px"
   },
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
